Routing:
[Gen][CCNA][CCNP Route][CCNP Remote][CCDA][MPLS][EEM][Add][Juniper][ACL][Host]
Switching: [CCNP Switch] [Switch Add] [Intro] [VLANs] [MLS] [STP] [QoS] [Availability] Security: [CCNP Security] [CCNA Security] [CCNP ISCW][PIX] [Adv PIX/ASA] [Net Sec1] [Net Sec2] Wireless: [Wireless] [Wireless Chall] [CCNA Wireless] [Wireless Theory] Voice: [Voice/QoS] [CCNA Voice] [CCVP Gateway] [CCVP Voice] Topics: [Dot1q][Dot1x][BGP][BRI][DHCP][IGRP][IGMP][OSPF][PPP][QoS][RADIUS][RIP][Subnet][SNMP][VLAN] Route SummarizationThis is a Route Summarization calculator [Theory][Test1][Test2]. Please enter some route values seperated by spaces (such as 1.2.3.4 1.2.3.5), or if it is a range, enter between "-" (such as 172.16.128.0-172.16.159.255): |
An example is:
Coding
// https://asecuritysite.com/cisco/routesum?net=172.16.128.0-172.16.159.255 // Examples: 172.16.128.0-172.16.159.255 -> 172.16.128.0 255.255.224.0 // 100.16.0.0 100.17.0.0 100.18.0.0 100.19.0.0 -> 100.16.0.0 255.252.0.0 // 151.100.1.0 151.100.2.0 151.100.3.0 151.100.4.0 151.100.5.0 151.100.6.0 151.100.7.0 -> 151.100.0.0/21 (151.100.0.0 255.255.248.0) //172.16.0.0-172.31.255.255 -> 172.16.0.0/12 // 172.16.50.0 172.16.60.0 172.16.70.0 172.16.80.0 172.16.90.0 172.16.100.0 172.16.110.0 172.16.120.0 -> 172.16.0.0 255.255.128.0 // 10.1.0.0 10.1.1.0 10.1.2.0 10.1.3.0 10.1.4.0 10.1.5.0 10.1.6.0 10.1.7.0 -> 10.1.0.0/21 // 172.1.4.0 172.1.4.128 172.1.5.0 172.1.6.0 172.1.7.0 -> 172.1.4.0/22 // 192.168.98.0 192.168.99.0 192.168.100.0 192.168.101.0 192.168.102.0 192.168.105.0 -> 192.168.96.0/20 // 192.168.128.0-192.168.159.0 -> 192.168.128.0/19/20 using System; using System.Text.RegularExpressions; using System.Collections; class MainClass { public static void Main (string[] args) { string range = "", tbIPout="", tbsumip="",tbsumm="",tbsumm0="",summ=""; // Define the IP address range put a dash for range, a space for specific addresses string inputip="192.168.0.1-192.168.0.101"; // string inputip="192.168.0.1 192.168.0.5 192.168.0.10"; string ip1 = Regex.Replace(inputip, "\r\n", " "); ArrayList add = new ArrayList(); string ipinput = ip1; if (ipinput.Contains("-")) { range = "yes"; ipinput = ipinput.Replace('-', ' '); } else range = "no"; ipinput = ipinput.Trim(); string[] ips = ipinput.Split(' '); foreach (string s in ips) { if (isip(s)) add.Add(s); } if (add.Count == 0) { tbIPout = "No IP addresses found"; return; } string subbits = getSummarization(add); string ipnet = gennetwork((string)add[0], tosubnet(subbits)); /// Calculate subnet /// if (range == "no") { tbsumip = ipnet; tbsumm = ipnet + "/" + subbits; tbsumm0 = tosubnet(subbits); } else { uint netA = convertIPtoInteger((string)ips[0]); uint netB = convertIPtoInteger((string)ips[1]); uint subA = netA ^ netB; subA = ~subA; string ba1 = Convert.ToString((subA & 0xff000000) >> 24); string ba2 = Convert.ToString((subA & 0xff0000) >> 16); string ba3 = Convert.ToString((subA & 0xff00) >> 8); string ba4 = Convert.ToString((subA & 0xff)); ip1 = ba1 + "." + ba2 + "." + ba3 + "." + ba4; ip1 = tosubnet(subbits); tbsumm0 = ip1; tbsumm = ipnet + "/" + subbits; tbsumip = ipnet; } foreach (string net1 in ips) { uint net = convertIPtoInteger(net1); string b1 = Convert.ToString((net & 0xff000000) >> 24); string b2 = Convert.ToString((net & 0xff0000) >> 16); string b3 = Convert.ToString((net & 0xff00) >> 8); string b4 = Convert.ToString((net & 0xff)); string ipnetwork = b1 + "." + b2 + "." + b3 + "." + b4; summ += toBitString(ipnetwork) + " (" + net1 + ")\r\n"; } Console.WriteLine("IP range: {0}",inputip); Console.WriteLine("Notes: {0}",tbIPout); Console.WriteLine("\n==Results==="); Console.WriteLine("IP Summarization: {0}",tbsumm); Console.WriteLine("\nIP address: {0}",tbsumip); Console.WriteLine("Subnet: {0}",tbsumm0); Console.WriteLine("\nSummarization:\n{0}",summ); } public static bool isip(string ip) { if (ip == null) return (false); string[] s = ip.Split('.'); bool valid = false; if (ip == "0.0.0.0" || ip == "0") return (true); try { int val1 = -1, val2 = -1, val3 = -1, val4 = -1; if (s.Length == 4) { try { val1 = System.Convert.ToInt32(s[0]); val2 = System.Convert.ToInt32(s[1]); val3 = System.Convert.ToInt32(s[2]); val4 = System.Convert.ToInt32(s[3]); if ((val1 > 0) && (val1 < 255) && (val2 >= 0) && (val2 <= 255) && (val3 >= 0) && (val3 <= 255) && (val4 >= 0) && (val4 <= 255)) valid = true; } catch (Exception e) { Console.WriteLine(e);}; } } catch (Exception e) { Console.WriteLine(e); }; return (valid); } public static bool issubnet(string ip) { if (ip == null) return (false); string[] sm = { "128.0.0.0","192.0.0.0", "224.0.0.0", "240.0.0.0","248.0.0.0","252.0.0.0","254.0.0.0","255.0.0.0", "255.128.0.0","255.192.0.0", "255.224.0.0", "255.240.0.0","255.248.0.0","255.252.0.0","255.254.0.0","255.255.0.0", "255.255.128.0","255.255.192.0", "255.255.224.0", "255.255.240.0","255.255.248.0","255.255.252.0","255.255.254.0","255.255.255.0", "255.255.255.128","255.255.255.192", "255.255.255.224", "255.255.255.240","255.255.255.248","255.255.255.252","255.255.255.254", "255.255.255.255"}; if (ip == "0") ip = "0.0.0.0"; if (ip == "0.0.0.0") return (true); ip = ip.Trim(); bool valid = false; try { for (int i = 0; i < sm.Length; i++) if (ip == sm[i]) valid = true; } catch (Exception e) { Console.WriteLine(e);}; return (valid); } public string invertSubnet(string w) { try { if (w == "any") return ("255.255.255.255"); uint ip1val = convertIPtoInteger(w); uint net1 = ip1val ^ 0xffffffff; uint val1 = (net1 & 0xff000000) >> 24; uint val2 = (net1 & 0x00ff0000) >> 16; uint val3 = (net1 & 0x0000ff00) >> 8; uint val4 = (net1 & 0x000000ff); return (val1 + "." + val2 + "." + val3 + "." + val4); } catch { }; return (w); } public static string gennetwork(string ip, string sub) { string s = ""; try { if (ip == null) return ("0.0.0.0"); if (sub == null) return ("0.0.0.0"); if (!isip(ip)) return ("0.0.0.0"); if (!issubnet(sub)) return ("0.0.0.0"); uint ip1val = convertIPtoInteger(ip); uint sub1val = convertIPtoInteger(sub); uint net1 = ip1val & sub1val; string b1 = Convert.ToString((net1 & 0xff000000) >> 24); string b2 = Convert.ToString((net1 & 0xff0000) >> 16); string b3 = Convert.ToString((net1 & 0xff00) >> 8); string b4 = Convert.ToString((net1 & 0xff)); s = b1 + "." + b2 + "." + b3 + "." + b4; return (s); } catch { }; return (s); } public static string getSummarization(ArrayList a) { if (a.Count == 0) return (""); int routesize = 0; bool found = true; for (int i = 0; i < 32; i++) { routesize = i; uint ipstart = convertIPtoInteger((string)a[0]); uint one = 1; ipstart = ipstart & (one << (31 - i)); foreach (string ip in a) { uint ip1val = convertIPtoInteger(ip); ip1val = ip1val & (one << (31 - i)); if (ip1val == ipstart) { } else found = false; if (found == false) break; } if (found == false) break; } // routesize = routesize; return (routesize.ToString()); } public static string tosubnet(string val) { switch (val) { case "0": return ("0.0.0.0"); case "1": return ("128.0.0"); case "2": return ("192.0.0.0"); case "3": return ("224.0.0.0"); case "4": return ("240.0.0.0"); case "5": return ("248.0.0.0"); case "6": return ("252.0.0.0"); case "7": return ("254.0.0.0"); case "8": return ("255.0.0.0"); case "9": return ("255.128.0.0"); case "10": return ("255.192.0.0"); case "11": return ("255.224.0.0"); case "12": return ("255.240.0.0"); case "13": return ("255.248.0.0"); case "14": return ("255.252.0.0"); case "15": return ("255.254.0.0"); case "16": return ("255.255.0.0"); case "17": return ("255.255.128.0"); case "18": return ("255.255.192.0"); case "19": return ("255.255.224.0"); case "20": return ("255.255.240.0"); case "21": return ("255.255.248.0"); case "22": return ("255.255.252.0"); case "23": return ("255.255.254.0"); case "24": return ("255.255.255.0"); case "25": return ("255.255.255.128"); case "26": return ("255.255.255.192"); case "27": return ("255.255.255.224"); case "28": return ("255.255.255.240"); case "29": return ("255.255.255.248"); case "30": return ("255.255.255.252"); case "31": return ("255.255.255.254"); case "32": return ("255.255.255.255"); default: return ("0.0.0.0"); } } public static string towildcard(string val) { switch (val) { case "0": return ("0.0.0.0"); case "1": return ("128.0.0"); case "2": return ("192.0.0.0"); case "3": return ("224.0.0.0"); case "4": return ("240.0.0.0"); case "5": return ("248.0.0.0"); case "6": return ("252.0.0.0"); case "7": return ("254.0.0.0"); case "8": return ("255.0.0.0"); case "9": return ("255.128.0.0"); case "10": return ("255.192.0.0"); case "11": return ("255.224.0.0"); case "12": return ("255.240.0.0"); case "13": return ("255.248.0.0"); case "14": return ("255.252.0.0"); case "15": return ("255.254.0.0"); case "16": return ("255.255.0.0"); case "17": return ("255.255.128.0"); case "18": return ("255.255.192.0"); case "19": return ("255.255.224.0"); case "20": return ("255.255.240.0"); case "21": return ("255.255.248.0"); case "22": return ("255.255.252.0"); case "23": return ("255.255.254.0"); case "24": return ("255.255.255.0"); case "25": return ("255.255.255.128"); case "26": return ("255.255.255.192"); case "27": return ("255.255.255.224"); case "28": return ("255.255.255.240"); case "29": return ("255.255.255.248"); case "30": return ("255.255.255.252"); case "31": return ("255.255.255.254"); case "32": return ("255.255.255.255"); default: return ("0.0.0.0"); } } public static uint convertIPtoInteger(string str) { string[] i = str.Split('.'); if (i.Length != 4) return (0); uint val1 = System.Convert.ToUInt32(i[0]); uint val2 = System.Convert.ToUInt32(i[1]); uint val3 = System.Convert.ToUInt32(i[2]); uint val4 = System.Convert.ToUInt32(i[3]); uint v = (val1 << 24) + (val2 << 16) + (val3 << 8) + val4; return (v); } public static string toBitString(string ip) { string con = ""; if (ip == null) ip = "0.0.0.0"; if (ip == "") ip = "0.0.0.0"; if (ip.Contains(".")) { string[] s = ip.Split('.'); if (s.Length == 4) { try { con += (string)intToBinary(System.Convert.ToInt32(s[0])) + "."; con += intToBinary(System.Convert.ToInt32(s[1])) + "."; con += intToBinary(System.Convert.ToInt32(s[2])) + "."; con += intToBinary(System.Convert.ToInt32(s[3])); } catch { } } } return (con); } public static string intToBinary(int val) { int[] mask = { 128, 64, 32, 16, 8, 4, 2, 1 }; string s = ""; for (int i = 0; i < 8; i++) { if ((val & mask[i]) == mask[i]) s += "1"; else s += "0"; } return (s); } }
Examples
You can call with a network range with the net parameter. For example: /cisco/routesum?net=192.168.128.0-192.168.159.0
or use spaces: /cisco/routesum?net=100.16.0.0 100.17.0.0 100.18.0.0 100.19.0.0